package com.dreamstations.changshads.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.activity.ArticleDetailActivity;
import com.dreamstations.changshads.activity.ArticleDetail_WebActivity;
import com.dreamstations.changshads.adapter.DSRvAdapter;
import com.dreamstations.changshads.entity.InvestigateBean;
import com.dreamstations.changshads.util.DSRequest;
import com.dreamstations.changshads.util.L;
import com.dreamstations.changshads.util.RequestType;
import com.squareup.picasso.Picasso;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.recyclerview.OnItemClickListener;

/**
 * 大事记述
 */
public class Investigate_Event_Frag extends BaseFragmentForHome<InvestigateBean> {

    private static final java.lang.String TAG = Investigate_Event_Frag.class.getName();

    public Investigate_Event_Frag() {
    }

    @SuppressLint("ValidFragment")
    public Investigate_Event_Frag(String title, @RequestType String reqType, int category) {
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("reqType", reqType);
        bundle.putInt(PARAM_TYPEID, category);
        setArguments(bundle);

    }

    @SuppressWarnings("WrongConstant")
    @NonNull
    @Override
    public DSRequest getDsRequest() {
        Bundle bundle = getArguments();
        return new DSRequest()
                .type(bundle.getString("reqType"))
                //二级分类
                .addParams(PARAM_TYPEID, String.valueOf(bundle.getInt(PARAM_TYPEID)))
                .setTag(Investigate_Event_Frag.this);
    }

    @NonNull
    @Override
    public DSRvAdapter.RvSupport<InvestigateBean> getItem() {
        return new DSRvAdapter.RvSupport<InvestigateBean>() {
            @Override
            public int getItemViewType(InvestigateBean data) {
                return 0;
            }

            @Override
            public ViewHolder createHolder(Context context, ViewGroup parent, int viewType) {
                final ViewHolder holder = ViewHolder.get(context, null, parent, R.layout.investigate_item, -1);
                return holder;
            }

            @Override
            public void convert(ViewHolder holder, InvestigateBean data) {
                holder.setText(R.id.subtitle, data.getTitle());
                ImageView imageView= holder.getView(R.id.inves_img_backround);
                L.i(TAG,"=>loadpic:"+DSRequest.SERVER_BASE1+data.getCover());
                Picasso.with(getActivity())
                        .load(DSRequest.SERVER_BASE1+data.getCover())
                        .error(R.drawable.img_default)
                        .into(imageView);
            }
        };
    }

    @Override
    public OnItemClickListener getOnItemClickListener() {
        return new OnItemClickListener<InvestigateBean>() {
            @Override
            public void onItemClick(ViewGroup parent, View view, InvestigateBean data, int position) {
                Intent intent = new Intent();
                if (data.getTemplate() == 1) {
                    intent.setClass(getActivity(), ArticleDetail_WebActivity.class);
                } else {
                    intent.setClass(getActivity(), ArticleDetailActivity.class);
                }
                intent.putExtra("data", data);
//                L.i(TAG,"=>dataId:"+data.getId());
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, InvestigateBean o, int position) {
                return false;
            }
        };
    }

    @Override
    public DSRvAdapter.RvSupport getHeader() {
        return null;
    }

    @Override
    public boolean getEnablePullLoadMore() {
        return true;
    }
}
